<template>
  <div class="login_container">
    <div class="login_cont">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets//logo.png" />
      </div>
      <!-- 标题 -->
      <h4 class="login_cont_title">后台电商管理登录</h4>
      <!-- 登录表单区域 -->
      <el-form class="login_formOk" :model="loginForm" :rules="loginRules" ref="loginFormReste">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input placeholder="用户名" prefix-icon="el-icon-user-solid" v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input type="password" placeholder="密码" prefix-icon="el-icon-lock" v-model="loginForm.password"></el-input>
        </el-form-item>
        <!-- 登录和重置按钮 -->
        <el-form-item class="btns">
          <el-button type="success" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      // 登录表单的数据绑定对象
      loginForm: {
        username: '',
        password: ''
      },
      // 登录表单的验证规则对象
      loginRules: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reset() {
      this.$refs.loginFormReste.resetFields()
    },
    login() {
      // 点击登录获取到表单数据
      this.$refs.loginFormReste.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        // 判断登录用户名和密码是否正确
        // 验证登录失败返回信息
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        // 登录成功跳转到后台主页路由
        this.$message.success('登录成功')
        // 生成token值
        window.sessionStorage.setItem('token', res.data.token)
        // 跳转到主页
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scope>
.login_container {
  position: relative;
  background-color: #41b883;
  height: 100%;
  .login_cont {
    width: 520px;
    height: 360px;
    background-color: #f2f2f2;
    border-radius: 18px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .avatar_box {
      width: 140px;
      height: 140px;
      border-radius: 50%;
      border: 1px solid #eee;
      box-shadow: 0 0 10px #ddd;
      padding: 10px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        background-color: #eee;
        border-radius: 50%;
      }
    }
  }
}
.login_formOk {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 50px;
  box-sizing: border-box;
  .el-input__inner {
    height: 50px;
    font-size: 16px;
  }
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
